package com.zhp.interceptor;

import com.zhp.config.TenantConfig;
import com.zhp.tenant.TenantContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhanghp
 * @since 2024/5/16 10:52
 */
public class RequestInterceptor implements HandlerInterceptor {

    @Resource
    private TenantConfig tenantConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TenantContextHolder.setTenantId(request.getHeader(tenantConfig.getRequestHeaderField()));
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        TenantContextHolder.clear();
    }
}
